using System.Collections.Generic;
using UnityEngine;

public class Bear : MonoBehaviour
{
	public enum State
	{
		idle = 0,
		attack = 1,
		move = 2,
		dash = 3
	}

	public Rigidbody2D rb;

	public Animator animator;

	public AudioSource audioSource;

	public Collider2D bodyCollider;

	public float speed;

	public float maxAttackInterval;

	public float minAttackInterval;

	public float maxActionInterval;

	public float minActionInterval;

	public float dashChance;

	public AudioClip[] attackAudios;

	public AudioClip[] hurtAudios;

	public bool canAttack;

	private float attackInterval = 1.2f;

	private bool isDash;

	private float _speed;

	private float move;

	[Header("Runtime")]
	public State state;

	public Vector3 movePos;

	public float actionCooldown;

	public float defaultScaleX;

	public float lastAttack;

	public List<Zombie> contactedZombies = new List<Zombie>();

	public Zombie targetZombie;

	private void Start()
	{
		canAttack = true;
		state = State.idle;
		_speed = speed;
		defaultScaleX = base.transform.localScale.x;
		move = 0f;
		Invoke("SetMove", 0f);
		Invoke("GoToPlayer", 0.1f);
	}

	public void GoToPlayer()
	{
		base.transform.position = CampaignGame.Instance.player.transform.position;
	}

	public void Update()
	{
		if (!bodyCollider.enabled)
		{
			bodyCollider.enabled = true;
		}
		if (state == State.attack && targetZombie != null)
		{
			FlipByPosition(targetZombie.transform.position);
			return;
		}
		if (Vector2.Distance(movePos, base.transform.position) > 0.1f)
		{
			state = State.move;
			Vector3 normalized = (base.transform.position - movePos).normalized;
			normalized.y *= 0.65f;
			base.transform.position -= normalized * 0.5f * _speed * Time.deltaTime;
			FlipByPosition(movePos);
		}
		else
		{
			isDash = false;
			state = State.idle;
			move = 0f;
		}
		animator.SetFloat("Move", move);
		CheckAndAttack();
	}

	public void SetMove()
	{
		movePos = CampaignGame.Instance.player.transform.position + new Vector3(Random.Range(-2f, 10f), Random.Range(-2f, 2f), 0f);
		if (dashChance > Random.value)
		{
			bodyCollider.enabled = false;
			_speed = speed * 5f;
			isDash = true;
			move = 2f;
			if (CampaignGame.Instance.zombies.Count > 0)
			{
				movePos = CampaignGame.Instance.zombies[Random.Range(0, CampaignGame.Instance.zombies.Count)].transform.position;
			}
			audioSource.PlayOneShot(attackAudios[Random.Range(0, attackAudios.Length)]);
		}
		else
		{
			_speed = speed;
			isDash = false;
			move = 1f;
		}
		movePos.y = Mathf.Min(-0.1f, Mathf.Max(-2.5f, movePos.y));
		Invoke("SetMove", Random.Range(minActionInterval, maxActionInterval));
	}

	public void FlipByPosition(Vector3 position)
	{
		Vector3 localScale = base.transform.localScale;
		if (position.x > base.transform.position.x)
		{
			localScale.x = defaultScaleX;
			base.transform.localScale = localScale;
		}
		else
		{
			localScale.x = 0f - defaultScaleX;
			base.transform.localScale = localScale;
		}
	}

	public void CheckAndAttack()
	{
		if (canAttack && !(Time.time - lastAttack < attackInterval) && contactedZombies.Count != 0 && !isDash)
		{
			state = State.attack;
			lastAttack = Time.time + Random.Range(minAttackInterval, maxAttackInterval);
			animator.SetTrigger("Attack");
			audioSource.PlayOneShot(attackAudios[Random.Range(0, attackAudios.Length)]);
			Invoke("AttackDone", 0.6f);
			if ((bool)contactedZombies[0] && (bool)contactedZombies[0].body && !contactedZombies[0].body.isDead)
			{
				targetZombie = contactedZombies[0];
				Invoke("AttackHurt", 0.5f);
			}
			else
			{
				contactedZombies.RemoveAt(0);
			}
		}
	}

	public void AttackHurt()
	{
		if (!targetZombie)
		{
			return;
		}
		Zombie zombie = targetZombie;
		zombie.body.Die();
		CampaignGame.Income(zombie.money);
		Vector3 normalized = (zombie.transform.position - base.transform.position).normalized;
		normalized.y = Mathf.Max(0f, normalized.y);
		BodyPart[] bodyParts = zombie.body.bodyParts;
		foreach (BodyPart bodyPart in bodyParts)
		{
			if (!bodyPart)
			{
				break;
			}
			if (bodyPart.name == "ChestRb" && zombie.body.isDead)
			{
				bodyPart.rb.AddForce(800f * normalized);
			}
			bodyPart.damageShare = Random.Range(0.1f, 1f);
			bodyPart.Bleed(bodyPart.transform.position);
		}
		audioSource.PlayOneShot(hurtAudios[Random.Range(0, hurtAudios.Length)]);
	}

	public void AttackDone()
	{
		state = State.idle;
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (!collision.TryGetComponent<Zombie>(out var component))
		{
			return;
		}
		if (!contactedZombies.Contains(component))
		{
			contactedZombies.Add(component);
		}
		if (isDash && move > 0f)
		{
			List<string> list = new List<string>();
			if (collision.transform.position.x >= base.transform.position.x)
			{
				list.Add("fallDownBack");
				component.Knockback(2000f, list[Random.Range(0, list.Count)]);
			}
			else
			{
				list.Add("fallDownFront");
				component.Knockback(0f, list[Random.Range(0, list.Count)]);
			}
		}
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		if (collision.TryGetComponent<Zombie>(out var component))
		{
			contactedZombies.Remove(component);
		}
	}
}
